package Algorithm.Tree;

/**
 * @Author cj
 * @Date 2022/8/30 20:00
 */
public class generateTree {

    static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int val) {
            this.value = val;
        }
    }

    // 有序数组生成平衡搜索二叉树
    public Node generateTree1(int[] sortArr) {
        if (sortArr == null) {
            return null;
        }
        return generate(sortArr, 0, sortArr.length - 1);
    }

    private Node generate(int[] sortArr, int start, int end) {
        if (start > end) {
            return null;
        }
        int mid = start + (end - start) / 2;
        Node root = new Node(sortArr[mid]);
        root.left = generate(sortArr, start, mid - 1);
        root.right = generate(sortArr, mid + 1, end);
        return root;
    }
}
